<template>
  <div>
    <MyTable :list="list">
      <template #header>
        <th>序号</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{row}"> 
        <td>{{row.id}}</td>
        <td>{{row.goods_name}}</td>
        <td>{{row.goods_price}}</td>
        <td>
          <input type="text" 
          v-if='row.inputVisible' 
          v-focus 
          v-model='row.inputValue' 
          @blur="row.inputVisible = false"
          @keydown.enter="keyDown(row)" 
          @keydown.esc="scope.row.inputValue = ''">
          <button 
          @click="row.inputVisible = true" 
          v-else 
          class="btn btn-primary btn-sm add-tag"
          style="display:block"
          >+Tag</button>
          <span v-for="(item,index) in row.tags" :key="index"  class="badge badge-warning">{{item}}</span>
        </td>
        <td><button @click="btn(row.id)" class="btn btn-danger btn-sm">删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../../components/MyTable.vue";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
   created(){
    this.$axios({
      url:'/api/goods'
    }).then(res =>{
      //  console.log(res)
       this.list = res.data.data
    }).catch(err =>{
      console.log(err)
    })
  },
  methods:{
     btn(id){
     this.list = this.list.filter(item => item.id !== id)
    },
    keyDown(row){
       if(row.inputValue.trim().length === 0) {
        alert('请输入内容!')
        return
      }
      row.tags.push(row.inputValue)
      row.inputValue = ''
    }
  }
};
</script>
<style scoped lang="less">
.my-goods-list {
  .badge {
    margin-right: 10px;
  }
}
span {
 margin-right: 8px;
}
button {
 margin-right: 8px
}
</style>